<?php
$params = '{"service":"api","path":"register","version":"1.0.0","username":"xiaoming","password":"123456","email":"xiaoming@email.com","code":"ABCD","phone":"1234567890123"}';
echo '原始的明文：<br>' . $params . '<br>';

$rsaPublicKey = file_get_contents('./rsa_public_key.pem');
$rsaPrivateKey = file_get_contents('./rsa_private_key.pem');

function encryptData($str, $publicKey) {
    $result = '';
    $chunks = str_split($str, 117);
    foreach ($chunks as $value) {
        openssl_public_encrypt($value, $crypted, $publicKey);
        $result .= base64_encode($crypted);
    }
    return $result;
}

function decryptData($data, $privateKey) {
    $result = '';
    $chunks = str_split($data, 172);
    foreach ($chunks as $value) {
        openssl_private_decrypt(base64_decode($value), $decrypted, $privateKey);
        $result .= $decrypted;
    }
    return $result;
}

$encryptedData = encryptData($params, $rsaPublicKey);
echo '加密后的结果：<br>' . $encryptedData . '<br>';

$decryptData = decryptData($encryptedData, $rsaPrivateKey);
echo '解密后的结果：<br>' . $decryptData . '<br>';
